package Math;

public class _263_UglyNumber {
    public boolean isUgly1(int num) {
        if (num == 0) {
            return false;
        }
        int cal = num;
        boolean canDiv = true;
        while (cal!=1) {
            if (!canDiv) {
                break;
            }
            if (cal % 2 == 0) {
                cal = cal/2;
            } else if (cal % 3 == 0) {
                cal = cal/3;
            } else if (cal % 5 == 0) {
                cal = cal / 5;
            } else {
                canDiv = false;
            }
        }
        if (cal == 1) {
            return true;
        } else {
            return false;
        }
    }

    //reference solution:faster,ignore above's judgement
    public boolean isUgly(int num) {
        if (num == 0) {
            return false;
        } else if (num == 1) {
            return true;
        } else {
            while (num % 2 == 0) {
                num/=2;
            }
            while (num%3 == 0) {
                num/=3;
            }
            while (num % 5 == 0) {
                num/=5;
            }
            return num==1;
        }
    }
}
